using Microsoft.AspNetCore.Mvc;
using WMS.RealTime.IServers.Interfaces;

namespace WMS.RealTime.API.Controllers
{
    /// <summary>
    /// 实时推送控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RealTimeController : ControllerBase
    {
        private readonly IRealTimePushService _realTimePushService;
        private readonly ILogger<RealTimeController> _logger;

        public RealTimeController(
            IRealTimePushService realTimePushService,
            ILogger<RealTimeController> logger)
        {
            _realTimePushService = realTimePushService;
            _logger = logger;
        }

        /// <summary>
        /// 推送今日概况数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-today-overview")]
        public async Task<ActionResult> PushTodayOverview()
        {
            try
            {
                await _realTimePushService.PushTodayOverviewAsync();
                return Ok(new { message = "今日概况数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送今日概况数据失败");
                return StatusCode(500, new { error = "推送今日概况数据失败" });
            }
        }

        /// <summary>
        /// 推送昨日概况数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-yesterday-overview")]
        public async Task<ActionResult> PushYesterdayOverview()
        {
            try
            {
                await _realTimePushService.PushYesterdayOverviewAsync();
                return Ok(new { message = "昨日概况数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送昨日概况数据失败");
                return StatusCode(500, new { error = "推送昨日概况数据失败" });
            }
        }

        /// <summary>
        /// 推送库存占比数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-inventory-proportion")]
        public async Task<ActionResult> PushInventoryProportion()
        {
            try
            {
                await _realTimePushService.PushInventoryProportionAsync();
                return Ok(new { message = "库存占比数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送库存占比数据失败");
                return StatusCode(500, new { error = "推送库存占比数据失败" });
            }
        }

        /// <summary>
        /// 推送入库统计数据
        /// </summary>
        /// <param name="timeRange">时间范围（天数）</param>
        /// <returns>推送结果</returns>
        [HttpPost("push-inbound-statistics")]
        public async Task<ActionResult> PushInboundStatistics([FromQuery] string timeRange = "7")
        {
            try
            {
                await _realTimePushService.PushInboundStatisticsAsync(timeRange);
                return Ok(new { message = $"入库统计数据推送成功（{timeRange}天）", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送入库统计数据失败");
                return StatusCode(500, new { error = "推送入库统计数据失败" });
            }
        }

        /// <summary>
        /// 推送待办事项数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-todo-items")]
        public async Task<ActionResult> PushTodoItems()
        {
            try
            {
                await _realTimePushService.PushTodoItemsAsync();
                return Ok(new { message = "待办事项数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送待办事项数据失败");
                return StatusCode(500, new { error = "推送待办事项数据失败" });
            }
        }

        /// <summary>
        /// 推送告警数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-alert-data")]
        public async Task<ActionResult> PushAlertData()
        {
            try
            {
                await _realTimePushService.PushAlertDataAsync();
                return Ok(new { message = "告警数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送告警数据失败");
                return StatusCode(500, new { error = "推送告警数据失败" });
            }
        }

        /// <summary>
        /// 推送排名数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-ranking-data")]
        public async Task<ActionResult> PushRankingData()
        {
            try
            {
                await _realTimePushService.PushRankingDataAsync();
                return Ok(new { message = "排名数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送排名数据失败");
                return StatusCode(500, new { error = "推送排名数据失败" });
            }
        }

        /// <summary>
        /// 推送系统消息
        /// </summary>
        /// <param name="message">系统消息内容</param>
        /// <returns>推送结果</returns>
        [HttpPost("push-system-message")]
        public async Task<ActionResult> PushSystemMessage([FromBody] string message)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    return BadRequest(new { error = "消息内容不能为空" });
                }

                await _realTimePushService.PushSystemMessageAsync(message);
                return Ok(new { message = "系统消息推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送系统消息失败");
                return StatusCode(500, new { error = "推送系统消息失败" });
            }
        }

        /// <summary>
        /// 推送所有数据
        /// </summary>
        /// <returns>推送结果</returns>
        [HttpPost("push-all-data")]
        public async Task<ActionResult> PushAllData()
        {
            try
            {
                await _realTimePushService.PushAllDataAsync();
                return Ok(new { message = "所有数据推送成功", timestamp = DateTime.Now });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送所有数据失败");
                return StatusCode(500, new { error = "推送所有数据失败" });
            }
        }

        /// <summary>
        /// 获取推送服务状态
        /// </summary>
        /// <returns>服务状态信息</returns>
        [HttpGet("status")]
        public ActionResult GetStatus()
        {
            return Ok(new
            {
                service = "RealTime Push Service",
                status = "Running",
                timestamp = DateTime.Now,
                endpoints = new[]
                {
                    "POST /api/realtime/push-today-overview",
                    "POST /api/realtime/push-yesterday-overview",
                    "POST /api/realtime/push-inventory-proportion",
                    "POST /api/realtime/push-inbound-statistics",
                    "POST /api/realtime/push-todo-items",
                    "POST /api/realtime/push-alert-data",
                    "POST /api/realtime/push-ranking-data",
                    "POST /api/realtime/push-system-message",
                    "POST /api/realtime/push-all-data"
                }
            });
        }
    }
} 